package com.mhz.widgets.client.filtertextbox;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Widget;

public class Filter extends Composite {

	@UiTemplate("Filter.ui.xml")
	interface ViewUiBinder extends UiBinder<Widget, Filter> {
	}

	private static ViewUiBinder uiBinder = GWT.create(ViewUiBinder.class);

	@UiField
	InlineLabel filterText;

	String filterName;

	RemoveFilterHandler handler;

	public Filter(String filterName) {
		this.filterName = filterName;
		initWidget(uiBinder.createAndBindUi(this));
		filterText.setText(filterName);
	}

	@UiHandler("filterText")
	public void removeFilter(ClickEvent event) {
		((HorizontalPanel) this.getParent()).remove(this);
		if (handler != null) {
			handler.onRemoveFilter(filterName);
		}
	}

	public interface RemoveFilterHandler {
		void onRemoveFilter(String filterName);
	}

	public void setRemoveFilterHandler(RemoveFilterHandler handler) {
		this.handler = handler;
	}
}
